package com.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class MyInterceptor implements HandlerInterceptor {

    public static final AntPathMatcher PATH_MATCHER=new AntPathMatcher();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        Object user = request.getSession().getAttribute("user");
        if (user!=null){
            log.info("放行路径"+uri);
            return true;
        }
        String[] urls={"/js/**","/imgs/**","/pages/login.html","/pages/query.html",
                "/bookReview/queryByBookName",
                "/user/**"
        };
        for (String url : urls) {
            if (PATH_MATCHER.match(url,uri)){
                log.info("放行路径"+uri);
                return true;
            }
        }
        response.sendRedirect("/pages/login.html");
        log.error("拦截路径"+uri);
        return false;
    }
}
